package com.rytong.tools.utils;

import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import org.apache.http.HttpStatus;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

public class HttpUtils {

	/**
	 * 下载图片
	 * @param url
	 * @return drawable
	 */
	public static Drawable loadImageFromUrl(String url) {
		URL m;
		InputStream i = null;
		try {
			m = new URL(url);
			i = (InputStream) m.getContent();
		}catch(Exception e) {
			Utils.printException(e);
		}
		Drawable d = Drawable.createFromStream(i, "src");
		return d;
	}

	/**
	 * 下载图片
	 * 
	 * @param imageUrl
	 * @return
	 * @throws Exception
	 */
	public static BitmapDrawable getBitmapByDownload(String imageUrl)
			throws Exception {
		BitmapDrawable bitmap = null;
		URL url = new URL(imageUrl);
		HttpURLConnection conn = (HttpURLConnection) url.openConnection();
		conn.setConnectTimeout(10*1000);
		InputStream is = conn.getInputStream();
		bitmap = new BitmapDrawable(is); // 创建图片
		conn.disconnect();
		return bitmap;
	}

	/**
	 * 检验网络连接
	 * 
	 * @return
	 */
	public static boolean is_Intent(Context context) {
		ConnectivityManager con = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo networkinfo = con.getActiveNetworkInfo();
		if (networkinfo == null || !networkinfo.isAvailable()) {
			// 当前网络不可用
			return false;
		}
		return true;
	}

	/**
	 * 网络下载图片
	 * @param url
	 * @return
	 */
	public static Bitmap downloadBitmap(String imageUrl) {
		URL url = null;
		Bitmap bitmap = null;
		HttpURLConnection conn = null;
		InputStream inputStream = null;
		try {
			url = new URL(imageUrl);
			conn = (HttpURLConnection) url.openConnection();
			conn.setConnectTimeout(10*1000);
			int statusCode = conn.getResponseCode();
			if (statusCode == HttpStatus.SC_OK) {
				inputStream = conn.getInputStream();
				if(null != inputStream){
					FlushedInputStream fit = new FlushedInputStream(inputStream);
					bitmap = BitmapFactory.decodeStream(fit);
				}
			}
		} catch (Exception e) {
			Utils.printException(e);
		} finally {
			if (inputStream != null) {
				try {
					inputStream.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				inputStream = null;
			}
			if(null != conn){
				conn.disconnect();				
			}
		}
		return bitmap;
	}

	/*
	 * An InputStream that skips the exact number of bytes provided, unless it
	 * reaches EOF.
	 */
	static class FlushedInputStream extends FilterInputStream {
		public FlushedInputStream(InputStream inputStream) {
			super(inputStream);
		}

		@Override
		public long skip(long n) throws IOException {
			long totalBytesSkipped = 0L;
			while (totalBytesSkipped < n) {
				long bytesSkipped = in.skip(n - totalBytesSkipped);
				if (bytesSkipped == 0L) {
					int b = read();
					if (b < 0) {
						break; // we reached EOF
					} else {
						bytesSkipped = 1; // we read one byte
					}
				}
				totalBytesSkipped += bytesSkipped;
			}
			return totalBytesSkipped;
		}
	}
}
